package com.intermancer.predictor.feeder;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;

import com.intermancer.predictor.data.ConsumeResponse;
import com.intermancer.predictor.data.Quantum;

public class OutputFCL implements FeedCycleListener {

	private static final String DEFAULT_OUTPUT_DATE_FORMAT = "yyyyMMdd";
	private int channelOffset;
	private PrintWriter outputWriter;
	private SimpleDateFormat dateFormat;
	
	public OutputFCL() {
		dateFormat = new SimpleDateFormat(DEFAULT_OUTPUT_DATE_FORMAT);
	}
	
	public boolean handle(ConsumeResponse consume,
			Quantum quantum) {
		StringBuffer outputString = new StringBuffer();
		outputString.append("Timestamp: ");
		outputString.append(dateFormat.format(quantum.getTimestamp()));
		outputString.append(" | Object channel value: ");
		outputString.append(quantum.getChannel(channelOffset).getValue());
		if (ConsumeResponse.CONSUME_COMPLETE.equals(consume)) {
			outputString.append(" | Final channel value: ");
			outputString.append(quantum.getLastChannel().getValue());
		}
		System.out.println(outputString.toString());
		if(outputWriter != null) {
			outputWriter.println(outputString.toString());
		}
		return true;
	}

	public void setChannelOffset(int objectOffset) {
		this.channelOffset = objectOffset;
	}

	public void setOutputWriter(PrintWriter outputWriter) {
		this.outputWriter = outputWriter;
	}

	public void init() {
	}
	
}
